/**
 * @projectName leetode
 * @package cn.leetcode
 * @className cn.leetcode.t572
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package cn.leetcode;

/**
 * t572
 * @description
 * @author fei
 * @date 2025/9/10 16:41
 * @version 1.0
 */
public class t572 {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        return compare(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }

    public boolean compare(TreeNode p, TreeNode q) {
        if (p == null || q == null) {
            return p == q;
        }
        return p.val == q.val && compare(p.left, q.left) && compare(p.right, q.right);
    }
}
